﻿using CardPlay.Model.ActionModel;
using NUnit.Framework;
using TechTalk.SpecFlow;

namespace CardPlay.Tests.Model.ActionModel
{
    [Binding]
    public class Steps
    {
        private static Calculator Calculator
        {
            get { return ScenarioContext.Current.Get<Calculator>(); }
        }

        [Given(@"I have a new calculator")]
        public void GivenIHaveANewCalculator()
        {
            ScenarioContext.Current.Set(new Calculator());
        }

        [When(@"I (add|multiply by) (\d+)")]
        public void WhenIPerform(string op, int argument)
        {
            var calculator = Calculator;

            var action = 
                op == "add" ? (IAction) new Calculator.AddOperation(calculator, argument) :
                op == "multiply by" ? new Calculator.MultiplyOperation(calculator, argument) : null;

            calculator.Execute(action);
        }

        [When(@"I (redo|reverse) (\d+) action(?:s)?")]
        public void WhenIReverse(string action, int actionCount)
        {
            while (actionCount-- > 0)
            {
                if (action == "redo")
                    Calculator.Operations.Redo();
                else
                    Calculator.Operations.Undo();
            }
        }

        [Then(@"the calculated value should be (\d+)")]
        public void ThenTheCalculatedValueShouldBe(int value)
        {
            Assert.AreEqual(value, Calculator.Result);
        }

        [Then(@"the calculator should have (\d+) changes?")]
        public void ThenTheCalculatorShouldHave(int actionCount)
        {
            Assert.AreEqual(actionCount, Calculator.Operations.TotalChanges);
        }

        [Then(@"I can('t)? (undo|redo)")]
        public void ThenICan(string invoke, string action)
        {
            Assert.AreEqual(
                invoke == string.Empty, 
                action == "undo" ? Calculator.Operations.CanUndo : Calculator.Operations.CanRedo);
        }
    }
}
